<?php
/************************************************************************************************
*   This file is a part of JV-LinkExchanger, a link exchange component for Joomla
*   Copyright (C) 2007-2008  Vijaykumar Niranjan 
*	
*   This program is free software; you can redistribute it and/or modify 
*   it under the terms of the GNU General Public License as published by 
*   the Free Software Foundation; either version 2 of the License, or 
*  (at your option) any later version. 
*
*   This program is distributed in the hope that it will be useful, 
*   but WITHOUT ANY WARRANTY; without even the implied warranty of 
*   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
*   GNU General Public License for more details. 
*
*   You should have received a copy of the GNU General Public License 
*   along with this program; if not, write to the Free Software 
*   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

* 	JV-LinkExchanger - Link Exchange Component for Joomla 1.0.x
* 	@package JV-LinkExchanger
* 	@file version 1.2.5 RELEASE 
* 	@author Vijaykumar Niranjan
* 	@link http://www.jv-extensions.com
* 	@copyright (C) 2007-2008 by Vijaykumar Niranjan
* 	@license http://www.gnu.org/copyleft/gpl.html GNU/GPL
*  	This program is distributed with the hope that it will be useful,
*  	but WITHOUT ANY WARRANTY; without even the implied warranty of
*  	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
***************************************************************************************************/
// ensure this file is being included by a parent file  
defined('_VALID_MOS') or die('Direct Access to this location is not allowed.'); 
#***********************************************************************************************
class Jvlinx_PostInstall
{
	function execNewinstall(&$retmsg)
	{
		global $database, $mosConfig_live_site, $mosConfig_absolute_path, $JVL, $JVG;
		
		$retmsg = '';
		
		// Load the file
		$filename = $mosConfig_absolute_path."/administrator/components/com_jvlinx/upgrade/create.sql";
		$fp = @fopen($filename, "r");
		if ($fp == NULL)
		{
			$retmsg = "Error while opening SQL instructions file:  ".$filename."<br />";
			return 0;
		}	

		// Parse the contents
		$sql = array();
		while (!feof($fp))
		{
			$data = trim(fgets($fp));
			if ((substr($data, 0, 3) == "---") || (strlen($data) < 5))
				continue;
			array_push($sql, $data);
		}				
		
		// Close the file
		fclose($fp);
		
		// Run all the SQL queries from that file
		for ($i=0;$i<count($sql);$i++)
		{
			$database->setQuery($sql[$i]);
			if (!$database->query())
			{
				$retmsg = "Error while running query: ".$sql[$i]."<br />";
				return 0;						
			}
		}

		return 1;
	}
//********************************************************************************************************	
	function execUpgrade($version, &$retmsg)
	{
		global $database, $mosConfig_absolute_path;
		$retmsg = '';
			
		// Load the file
		$filename = $mosConfig_absolute_path."/administrator/components/com_jvlinx/upgrade/up".$version.".sql";
		$fp = @fopen($filename, "r");
		if ($fp == NULL)
			return Jvlinx_PostInstall::updateVersionInDatabase($version);
		
		// Parse the contents
		$sql = array();
		while (!feof($fp))
		{
			$data = trim(fgets($fp));
			if ((substr($data, 0, 3) == "---") || (strlen($data) < 5))
				continue;
			array_push($sql, $data);
		}				
		
		// Close the file
		fclose($fp);
		 
		// Run all the SQL queries from that file
		for ($i=0;$i<count($sql);$i++)
		{
			$database->setQuery($sql[$i]);
			if (!$database->query())
			{
				$retmsg = "Error while running query: ".$sql[$i]."<br />";
				return 0;						
			}
		}

		return Jvlinx_PostInstall::updateVersionInDatabase($version);
	}
//********************************************************************************************************	
	function updateVersionInDatabase($version)
	{
		global $database;
		
		// Update table version to upgraded version
		$database->setQuery("update `#__jvlinx_settings` set `version` = '".$version."'");
		if (!$database->query())
			return 0;					
		
		return 1;
	}
//********************************************************************************************************
}
?>